package com.dy.project.service.impl;

import com.dy.project.domain.CommodityOfflineUser;
import com.dy.project.exception.CustomException;
import com.dy.project.mapper.UserMapper;
import com.dy.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public CommodityOfflineUser login(CommodityOfflineUser commodityOfflineUser) {
        CommodityOfflineUser loginUser = userMapper.getUserByUName(commodityOfflineUser);
        if(loginUser == null) {
            throw new CustomException("用戶名或密碼錯誤");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        if(passwordEncoder.matches(commodityOfflineUser.getCouPassword(),loginUser.getCouPassword())) {
            loginUser.setCouPassword(null);
            return loginUser;
        }
        throw new CustomException("用戶名或密碼錯誤");
    }
}
